package org.nimi317.web_gis.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.date.DateUtil;
import org.nimi317.web_gis.Enum.DistanceEnum;
import org.nimi317.web_gis.Enum.InterpolateEnum;
import org.nimi317.web_gis.Enum.KrigingModelEnum;
import org.nimi317.web_gis.Enum.RoleName;
import org.nimi317.web_gis.exception.E;
import org.nimi317.web_gis.exception.RException;
import org.nimi317.web_gis.form.post.InterpolateIdwPost;
import org.nimi317.web_gis.form.post.InterpolatePost;
import org.nimi317.web_gis.form.post.InterpolateSearchPost;
import org.nimi317.web_gis.form.post.Page;
import org.nimi317.web_gis.entity.R;
import org.nimi317.web_gis.service.InterpolateService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author thunderobot
 */
@RestController
@RequestMapping("interpolate")
@SaCheckLogin
public class InterpolateController {

    private final InterpolateService service;

    public InterpolateController(InterpolateService service) {
        this.service = service;
    }

    @PostMapping("kriging")
    public R<Void> post(@Validated @RequestPart InterpolatePost post, @RequestPart MultipartFile file) {
        check(post.getTime());
        service.saveInterpolate(post, file);
        return R.ok();
    }

    @PostMapping("search/kriging")
    public R<String> get(@Validated @RequestBody InterpolateSearchPost post) {
        check(post.getTime());
        String searched = service.searchInterpolate(post);
        return R.ok(searched);
    }

    @PostMapping("idw")
    public R<Void> idw(@Validated @RequestPart InterpolateIdwPost post, @RequestPart MultipartFile file) {
        check(post.getTime());
        service.saveInterpolate(post, file);
        return R.ok();
    }


    @PostMapping("search/idw")
    public R<String> idwSearch(@Validated @RequestBody InterpolateIdwPost post) {
        check(post.getTime());
        String searched = service.searchInterpolate(post);
        return R.ok(searched);
    }

    @PostMapping("page")
    @org.nimi317.web_gis.annotation.Page
    @SaCheckRole(RoleName.ADMIN)
    public R<?> page(@Validated @RequestBody Page page) {
        return R.ok(service.pageInterpolate(page));
    }

    @GetMapping("dict")
    public R<?> dict() {
        List<? extends Map<String, ?>> maps = Arrays.stream(InterpolateEnum.values()).map(InterpolateEnum::getObject).toList();
        List<? extends Map<?, ?>> kriging = Arrays.stream(KrigingModelEnum.values()).map(KrigingModelEnum::getObject).toList();
        List<? extends Map<?, ?>> idw = Arrays.stream(DistanceEnum.values()).map(DistanceEnum::getObject).toList();
        return R.ok(Map.of("type", maps, "kriging", kriging, "idw", idw));
    }

    /**
     * 检查时间是否合法 时间需要满足要求为小时级别 不存在分钟以及秒
     *
     * @param time 时间
     */
    private void check(Date time) {
        int minute = DateUtil.minute(time);
        int second = DateUtil.second(time);
        if (minute != 0 || second != 0) {
            throw new RException(E.TimeNotLegitimateException);
        }
    }
}
